﻿namespace MatrixProgram
{
    using System;
    using System.Text;

    [VersionAttribute(1.0)]
    public class Matrix<T>
    {
        private const int DefaultSize = 12;
        private T[,] matrix;

        public Matrix()
        {
            this.matrix = new T[DefaultSize, DefaultSize];
        }

        public Matrix(int row, int col)
        {
            if (row < 0 || col < 0)
            {
                throw new ArgumentOutOfRangeException("Row and Col must be positive.");
            }
            else
            {
                this.Row = row;
                this.Col = col;
                this.matrix = new T[row, col];
            }
        }

        public Matrix(T[,] matrix)
        {
            this.matrix = matrix;
            this.Row = matrix.GetLength(0);
            this.Col = matrix.GetLength(1);
        }

        public int Row { get; set; }

        public int Col { get; set; }

        public T this[int row, int col]
        {
            get
            {
                if (this.Row < row || this.Col < col || row < 0 || col < 0)
                {
                    throw new ArgumentOutOfRangeException();
                }
                else
                {
                    return this.matrix[row, col];
                }
            }

            set
            {
                if (this.Row < row || this.Col < col || row < 0 || col < 0)
                {
                    throw new ArgumentOutOfRangeException();
                }
                else
                {
                    this.matrix[row, col] = value;
                }
            }
        }

        public override string ToString()
        {
            StringBuilder matrixString = new StringBuilder();
            for (int r = 0; r < this.Row; r++)
            {
                for (int c = 0; c < this.Col; c++)
                {
                    matrixString.Append(this.matrix[r, c] + " ");
                }

                matrixString.AppendLine();
            }

            return matrixString.ToString();
        }
    }
}